<?php

include dirname(__FILE__). '/AController.php';

class UserController extends AController
{
	/**
	 * @var string the default layout for the views. Defaults to '//layouts/column2', meaning
	 * using two-column layout. See 'protected/views/layouts/column2.php'.
	 */
	public $layout='/layouts/column2';

	/**
	 * @return array action filters
	 */
	public function filters()
	{
		return array(
			'accessControl', // perform access control for CRUD operations
		);
	}

	/**
	 * Specifies the access control rules.
	 * This method is used by the 'accessControl' filter.
	 * @return array access control rules
	 */
	public function accessRules()
	{
		return array(
			array('allow',  // allow all users to perform 'index' and 'view' actions
				'actions'=>array('index','view'),
				'users'=>array('*'),
			),
			array('allow', // allow authenticated user to perform 'create' and 'update' actions
				'actions'=>array('create','update', 'list', 'ownerList'),// TODO 这里到时要将 ownerList 放到 admin 的用户组去
				'users'=>array('@'),
			),
			array('allow', // allow admin user to perform 'admin' and 'delete' actions
				'actions'=>array('admin','delete', 'ownerList'),
				'users'=>array('admin'),
			),
			array('deny',  // deny all users
				'users'=>array('*'),
			),
		);
	}
	
	public function actionUpdate() {
		if (key_exists('accept', $_GET) && !empty($_POST['users'])) {
			$trans = Yii::app()->db->beginTransaction();
			try {
				foreach ($_POST['users'] as $k => $user_id) {
					$user = User::model()->find('user_id=:user_id', array(':user_id' => $user_id));
					
					$num = 0;
					if ($_GET['accept'] != 'Y' && $user->accept == 'Y') {
						$num = -1;
					} else if ($_GET['accept'] == 'Y' && $user->accept != 'Y') {
						$num = 1;
					}
					
					$owner = Owner::model()->find('loginid=:owner_id', array(':owner_id' => $user->owner_id));
					$owner->vjoinmb += $num;
					$owner->save();
					
					$website = Website::model()->findByPk($user->website_id);
					$website->vjoinmb += $num;
					$website->save();
					
					$adv = Adv::model()->find('adv_id=:adv_id', array(':adv_id' => $user->adv_id));
					$adv->vjoinmb += $num;
					$adv->save();
					
					//在diary_log表中，找到会员注册当天的数据， 更新vjoinmb 
					$diary_log = DiaryLog::load($user->owner_id, $adv->website_id, date('Y-m-d', strtotime($user->date_join)));
					$diary_log->vjoinmb += $num; 
					$diary_log->save();
					
					//在diary_log表中，找到当天的数据，更新 check_joinmb
					$diary_log = DiaryLog::load($user->owner_id, $adv->website_id);
					$diary_log->check_joinmb += $num; 
					$diary_log->save();
					
					$user->accept = $_GET['accept'];
					$user->save();
				}
				$trans->commit();
			} catch (Exception $e) {
				$trans->rollback();
			}
		}
		$this->redirect(array('list'));
	}
	
	public function actionList() {
		$block_title = "会员列表";
		$arr = array(
			'pagination' => array('pageSize' => 20),
			'criteria' => array('order' => 'date_join DESC', 'condition' => '1')
		);
		if (key_exists('accept', $_GET)) {
			$arr['criteria']["condition"] .= " AND accept='{$_GET['accept']}'";// TODO 这里到时要对 $_GET['accept'] 进行安全处理
			$str = array('Y' => ' - 已认可', 'N' => ' - 已否决', 'P' => ' - 待审核');
			$block_title .= $str[$_GET['accept']];
		}
		if (!empty($_GET['owner_id'])) {
			$arr['criteria']['condition'] .= " AND owner_id='{$_GET['owner_id']}'";// TODO 这里到时要对 $_GET['owner_id'] 进行安全处理
		}
		$dataProvider = new CActiveDataProvider('user', $arr);
		$this->render('list', array('block_title' => $block_title, 'dataProvider' => $dataProvider));
	}
	
	/**
	 * 来自某个推广账号的会员列表
	 */
	public function actionOwnerList() {
		$this->layout = '/layouts/popMain';
		$this->actionList();
	}

	/**
	 * Returns the data model based on the primary key given in the GET variable.
	 * If the data model is not found, an HTTP exception will be raised.
	 * @param integer the ID of the model to be loaded
	 */
	public function loadModel($id)
	{
		$model=User::model()->findByPk($id);
		if($model===null)
			throw new CHttpException(404,'The requested page does not exist.');
		return $model;
	}
}
